<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>animate</title>
    <style>
        ul {
            position: relative;
            top: 100px;
        }

        li {
            position: absolute;
        }

        li:nth-child(2) {
            position: absolute;
            top: 20px;
        }
    </style>
</head>

<body>
    <ul>
        <li>1</li>
        <li>2</li>
    </ul>
    <button>移动1</button>
    <button>移动2</button>
</body>
<script>
    let li = document.querySelector('li:first-child');
    let li2 = document.querySelector('li:nth-child(2)');
    function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(() => {
            let step = (target - obj.offsetLeft) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (obj.offsetLeft == target) {
                clearInterval(obj.timer);
            }
            obj.style.left = obj.offsetLeft + step + 'px';
        }, 15)
    }
    // console.log(document.querySelectorAll('button'))
    document.querySelector('button:first-of-type').addEventListener('click', () => {
        animate(li, 500);
    })
    document.querySelector('button:nth-of-type(2)').addEventListener('click', () => {
        animate(li, 1000);
    })
</script>

</html>